<?php
/**
 * admin service file Created by PhpStorm.
 * User: yang
 * Date: 2020/04/26
 */

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Http\Service\Admin\RolesService;

class RolesController extends Controller
{
    /**
     * 视图层
     *
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function index()
    {
        return view('Admin.roles.index');
    }

    /**
     * 获取列表数据
     *
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function list(Request $request)
    {
        $get_data = $request->all();

        $result = RolesService::getList($get_data);

        return response()->json($result);
    }

    /**
     * 添加视图
     *
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function create()
    {
        return view('Admin.roles.create');
    }

    /**
     * 添加操作
     *
     * @param Request $request
     * @return array
     */
    public function insert(Request $request)
    {
        $post_data = $request->except(['_token']);

        return RolesService::insert($post_data);
    }

    /**
     * 编辑视图
     *
     * @param Request $request
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function edit(Request $request)
    {
        $id = $request->get('id', 0);

        $result = RolesService::edit((int)$id);

        return view('Admin.roles.edit', $result);
    }

    /**
     * 编辑操作
     *
     * @param Request $request
     * @return array
     */
    public function update(Request $request)
    {
        $post_data = $request->except(['_token']);

        return RolesService::update($post_data);
    }

    /**
     * 删除操作
     *
     * @param Request $request
     * @return mixed
     */
    public function delete(Request $request)
    {
        $id = $request->get('id', 0);

        return RolesService::delete((int)$id);
    }

    /**修改状态
     *
     * @param Request $request
     * @return array
     */
    public function status(Request $request)
    {
        $post_data = $request->only(['id', 'status']);

        return RolesService::editStatus($post_data);
    }

    /**
     * 权限视图
     *
     * @param Request $request
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function powerList(Request $request)
    {
        $id = $request->get('id', 0);

        $data = RolesService::getPowerList($id);
        $result = [
            'role_id' => $id,
            'data' => $data['data']
        ];

        return view('Admin.roles.power', $result);
    }

    /**
     * 权限操作
     * @param Request $request
     * @return array
     */
    public function power(Request $request)
    {
        $post_data = $request->except(['_token']);

        shi_wu_start();
        $res = RolesService::power($post_data);
        shi_wu_end($res);

        return $res;
    }
}